package com.example.cinema.drawview;

import android.animation.Animator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.cinema.bindingdata.PlayerData;

import java.io.IOException;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import tv.danmaku.ijk.media.player.MediaInfo;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

//影片播放组件
public class VideoPlayer extends FrameLayout {
    public VideoPlayer(@NonNull Context context) {
        this(context,null);
    }

    public VideoPlayer(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    private Context context;
    public VideoPlayer(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private SurfaceView surfaceView;
    private void init() {

        surfaceView = new SurfaceView(context);
        surfaceView.getHolder().addCallback(new Callback());
        LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
        surfaceView.setLayoutParams(params);
        addView(surfaceView);

        loadVideo(playurl);
    }
    private String playurl;
    public void SetUrlPlayer(String url)
    {
        this.playurl = url;
    }




    public IjkMediaPlayer mediaPlayer;
    private int w =0;
    private int h =0;
    private int width = 0;
    private int heigh = 0;
    public void loadVideo(String playurl)
    {
        this.playurl = playurl;
        if(playurl == null)
        {
            return;
        }

        if(mediaPlayer != null)
        {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
        mediaPlayer = new IjkMediaPlayer();

        try {
            mediaPlayer.setDataSource(playurl);
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.setDisplay(surfaceView.getHolder());
        mediaPlayer.prepareAsync();
        mediaPlayer.start();
        //播放器信息侦听
        mediaPlayer.setOnInfoListener(new IMediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(IMediaPlayer iMediaPlayer, int i, int i1) {
                if(infolistener!=null)
                {
                    infolistener.PlayerInfo(iMediaPlayer.getDuration());
                    Log.i("HHHH",iMediaPlayer.getDuration()+"");

                }
                return false;
            }
        });
        //准备准听器
        mediaPlayer.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(IMediaPlayer iMediaPlayer) {




                width = getMeasuredWidth();
                heigh = getMeasuredHeight();
                Log.i("JJJ", "高度1:" + heigh);
                int videowidth = iMediaPlayer.getVideoWidth();
                int videoheigh = iMediaPlayer.getVideoHeight();
                Log.i("JJJ", "宽度" + videowidth);
                Log.i("JJJ", "高度" + videoheigh);
                int hh = 0;
                int ww = 0;
                if((videowidth/16*9)==videoheigh)
                {
                    ww = heigh*16/9;
                    hh = heigh;
                    Log.i("NNN","执行转换0");
                }else {
                    ww = heigh*16/9;
                    hh = heigh;
                }

                Log.i("NNN",videowidth*16/9+"");
                Log.i("NNN",videowidth*16/9+"");
                Log.i("NNN",ww+"");
                Log.i("NNN",hh+"");
                LayoutParams params = new LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
                params.height = hh;
                params.width  = ww;

                params.gravity = Gravity.CENTER;
                Log.i("RRRR",ww+"");
                Log.i("RRRR",hh+"");
                surfaceView.setLayoutParams(params);
                mediaPlayer.setDisplay(surfaceView.getHolder());
            }
        });
        //帧监听

    }
    //影片时长
    public long duration;
    //开始播放
    public void startplay()
    {
        mediaPlayer.start();
    }
    //暂停播放
    public void shtopplay()
    {
        mediaPlayer.pause();
    }
    //获取播放状态
    public boolean getplaystate(){
        return mediaPlayer.isPlaying();
    }
    //获取当前播放时长
    public long current()
    {
        return mediaPlayer.getCurrentPosition();
    }
    //设置进度
    public void setProess(long duration)
    {
        mediaPlayer.seekTo(duration);
    }
    //全屏
    private int pheigh = 0;
    public void allquan()
    {
        if(pheigh<1)
        {

            pheigh = getMeasuredHeight();
        }


        int w1 = width = getResources().getDisplayMetrics().widthPixels;
        int h1 = heigh = getResources().getDisplayMetrics().heightPixels;
        Log.i("JJJ", "屏幕宽度" + w1);
        Log.i("JJJ", "屏幕高度" + h1);
        int videowidth = mediaPlayer.getVideoWidth();
        int videoheigh = mediaPlayer.getVideoHeight();
        Log.i("JJJ", "宽度" + videowidth);
        Log.i("JJJ", "高度" + videoheigh);
        int ww = 0;
        int hh = 0;
        ww = w1*16/9;
        hh = w1;


        Log.i("JJJ", "宽度ww:" + ww);
        Log.i("JJJ", "高度hh:" + hh);
        ViewGroup.LayoutParams pram = getLayoutParams();
        pram.height = w1;
        pram.width = h1;
        this.setLayoutParams(pram);

        LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
        params.gravity = Gravity.CENTER;
        params.height = hh;
        params.width  = ww;

        params.gravity = Gravity.CENTER;

        surfaceView.setLayoutParams(params);
        mediaPlayer.setDisplay(surfaceView.getHolder());
    }


    //缩屏
    public void narrow(int height)
    {
        int www = getResources().getDisplayMetrics().widthPixels;
        int hhh = getResources().getDisplayMetrics().heightPixels;
        Log.d("GGGG", "narrow: www"+www);
        Log.d("GGGG", "narrow: hhh"+hhh);
        int w = mediaPlayer.getVideoWidth();
        int h = mediaPlayer.getVideoHeight();
        Log.d("GGGG", "narrow: heigh:"+heigh);
        int ww = pheigh*16/9;
        int hh = pheigh;
        Log.d("GGGG", "narrow: w:"+ww);
        Log.d("GGGG", "narrow: h:"+hh);


        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = height;
        params.width = hhh;
        setLayoutParams(params);

        LayoutParams params1 = new LayoutParams(MATCH_PARENT,WRAP_CONTENT);
        params1.height = hh;
        params1.width = ww;
        params1.gravity = Gravity.CENTER;
        surfaceView.setLayoutParams(params1);
        mediaPlayer.setDisplay(surfaceView.getHolder());
    }
    /**********************************/
    class Callback implements SurfaceHolder.Callback{

        @Override
        public void surfaceCreated(@NonNull SurfaceHolder holder) {
            if (mediaPlayer.isPlaying())
            {
                mediaPlayer.setDisplay(surfaceView.getHolder());
            }
        }

        @Override
        public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
            mediaPlayer.setDisplay(surfaceView.getHolder());
        }

        @Override
        public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

        }
    }

    /**********************************/
    public  interface VideoPlayerInfo{
        void PlayerInfo(long total);//视频长度

    }

    private VideoPlayerInfo infolistener;
    public void setOnVideoPlayerInfo(VideoPlayerInfo info)
    {
        infolistener = info;
    }
}
